package com.cardbaobao.cardbabyclient.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.cardbaobao.cardbabyclient.model.PhoneVersion;
import com.meituan.android.walle.WalleChannelReader;

import java.util.UUID;


/**
 * 获取手机信息工具类
 * Created by zad
 * 2016/7/27 10:41
 */

public class PhoneInfoUtil {
    /**
     * 获取手机版本型号信息
     * @return
     */
    public static PhoneVersion getPhoneVersionInfo(Context context) {
        PhoneVersion version = new PhoneVersion();
        try {
                version.setPhone_sys("android");// android系统
                version.setSys_version(android.os.Build.VERSION.RELEASE);// 系统版本
                version.setModel(android.os.Build.BRAND + " " + android.os.Build.MODEL);// 手机model机型
                version.setUniqueId(getUniqueId(context));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return version;
    }

    /**
     * 手机唯一标识
     * @param context
     * @return
     */
    public static String getUniqueId(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String imei = tm.getDeviceId();

        String simSerialNumber = tm.getSimSerialNumber();
        String androidId = android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
        UUID deviceUuid = new UUID(androidId.hashCode(), ((long) imei.hashCode() << 32) | simSerialNumber.hashCode());
        String uniqueId = deviceUuid.toString();
        return uniqueId;
    }

    /**
     * 获取meta-data名称对应的value值 name meta-data名称
     * @return 指定meta-data的string值
     */
    public static String getMetaData(Context context, String name) {
        try {
            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            String value = appInfo.metaData.getString(name);
            return value;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取打包APK渠道号
     * @return 友盟多渠道打包时指定的渠道号
     */
    public static String getApkChannelName(Context mContext) {
        String value= WalleChannelReader.getChannel(mContext.getApplicationContext());
        return TextUtils.isEmpty(value)?"cbb":value;
    }

    /**
     * 获取当前安装APP的Version
     * @return app当前版本号
     */
    public static int getVersion(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            return info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return 1;//默认返回最小版本号
        }
    }
}
